package org.adream.account.controller;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.adream.account.entity.HelpEntity;
import org.adream.account.rest.api.JsonResult;
import org.adream.account.service.ProductService;
import org.adream.account.util.FileUtils;
import org.adream.account.util.UUIDUtil;
import org.adream.account.util.Utils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class HelpController{
	
	private static Logger log = LoggerFactory.getLogger(HelpController.class);

	@Autowired
	private ProductService productService;
	
	@Value("${image.file.path.helppic}")
	private String helpPicPath; 
	  
	/**
     * 前端显示所有模块
     * @param
     * @return
     */
    @RequestMapping(value = "/listAllTitle", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JsonResult listAllTitle(HttpServletResponse response) {
        response.setHeader("Access-Control-Allow-Origin", "*");
        List<HelpEntity> allTitleLists = productService.allProduct();
        return new JsonResult(allTitleLists);
    }
    
    /**
     * 后端增加所有模块
     * @param
     * @return
     */
    @RequestMapping(value = "/addproduce", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JsonResult addProduce(HttpServletResponse response,@RequestParam("name")String name,@RequestParam("productid")String productid,@RequestParam("imgurl")String imgurl,@RequestParam("tucaourl")String tucaourl,@RequestParam("sort")String sort) {
    	response.setHeader("Access-Control-Allow-Origin", "*");
    	productService.addproduct(productid,imgurl, name, sort, tucaourl);
        return new JsonResult();
    }
    
    /**
     * 后端根据id查询
     * @param 
     * @return
     */
    @RequestMapping(value = "/selectproduct", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JsonResult selectProduce(@RequestParam("id")String id) {
    	int helpid = Integer.parseInt(id);
        HelpEntity helpEntity = productService.selectProduct(helpid);
        return new JsonResult(helpEntity);
    }
    
    /**
     * 后端根据id查询
     * @param 
     * @return
     */
    @RequestMapping(value = "/deleteproduct", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JsonResult deleteProduce(@RequestParam("id")String id) {
    	int helpid = Integer.parseInt(id);
    	productService.deleteProduct(helpid);
        return new JsonResult();
    }
    
    /**判断模块名称是否有重复
     * @param modulename
     * @return
     */
    @RequestMapping(value ="/isnameexsit", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public JsonResult isNameExsit(@RequestParam("modulename")String modulename) {
    	boolean isName=	productService.isName(modulename);
    	if(isName) {//有重复名称
    		return new JsonResult();
    	} else {
    		return new JsonResult("没有重复数据");
    	}
    }
    
    /**
     * 修改信息
     * @param 
     * @return
     */
    @RequestMapping(value = "/updateproduce")
    @ResponseBody
    public Object updateproduce(@RequestBody HelpEntity list) {
    	int id = list.getId();
    	String name = list.getName();
    	String sort = list.getSort();
    	String tucaourl = list.getTucaourl();
    	if("".equals(list.getImgurl())) {
    		String productid = list.getProductid();
    		productService.updateProduct(id, productid,name, sort, tucaourl);
    	} else {
    		String imgurl = list.getImgurl();
    		productService.updateimgProduct(id, name, sort, tucaourl,imgurl);
    	}    	   	
    	 return new JsonResult();
    }
    
    /**
     * 上传图片
     * @param
     * @return
     */
    @RequestMapping(value = "/uploadImg", method = RequestMethod.POST, produces = "application/json;charset=UTF-8")
    @ResponseBody
    public String upimg(@RequestParam("file")MultipartFile file) {
    	String path = helpPicPath.endsWith(File.separator) ? helpPicPath : helpPicPath + File.separator;		
    	String oriFileName = file.getOriginalFilename();
    	//获取文件名后缀
		String ext = oriFileName.substring(oriFileName.lastIndexOf(".") + 1);
		String fileName = Utils.parseDateToYearMonthDay(new Date()) + "-" + UUIDUtil.createUUID() + "." + ext;
		String fileName2 = path + fileName;
		File file1 = new File(path);
		if(!file1.exists()) {
			file1.mkdir(); 
		}
		try {
			FileUtils.writeToFile(new File(fileName2), file.getInputStream());
		} catch (IOException e) {
			e.printStackTrace();
		}
        return fileName;
    }
    
    /**
     * 编辑页面
     * @return
     */
    @RequestMapping(value = "/helpEditPage")
	public ModelAndView helpEditPage() {
		ModelAndView mv = new ModelAndView("/help/help_edit");
		return mv;
	}
    
    /**
     * 增加页面
     * @return
     */
    @RequestMapping(value = "/helpAddPage")  
	public ModelAndView helpaddPage() {
		ModelAndView mv = new ModelAndView("/help/help_add");
		return mv;
	}
    
    /**
     * 后台页面
     * @return
     */
    @RequestMapping(value = "/helpPage")  
	public ModelAndView helpPage() {
		ModelAndView mv = new ModelAndView("/help/help");
		return mv;
	}
}